package site.isscloud.jt809;

// VehicleControlCommand.java

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.text.SimpleDateFormat;
import java.util.Date;

public class VehicleControlCommand {
    private byte commandType;
    private long startTime;
    private long endTime;
    private int regionId;
    private String[] vehicleNos;

    public VehicleControlCommand(byte commandType, long startTime, long endTime, 
                                int regionId, String[] vehicleNos) {
        this.commandType = commandType;
        this.startTime = startTime;
        this.endTime = endTime;
        this.regionId = regionId;
        this.vehicleNos = vehicleNos;
    }

    public byte[] toByteArray() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            // COMMAND_TYPE
            baos.write(commandType);
            
            // START_TIME (6 bytes)
            baos.write(JT809Utils.longToByteArray(startTime) );
            
            // END_TIME (6 bytes)
            baos.write(JT809Utils.longToByteArray(endTime));
            
            // REGION_ID (4 bytes)
            baos.write((regionId >> 24) & 0xFF);
            baos.write((regionId >> 16) & 0xFF);
            baos.write((regionId >> 8) & 0xFF);
            baos.write(regionId & 0xFF);
            
            // VEHICLE_LIST (each vehicle 21 bytes)
            for (String vehicleNo : vehicleNos) {
                byte[] vehicleBytes = new byte[21];
                byte[] srcBytes = vehicleNo.getBytes("GBK");
                System.arraycopy(srcBytes, 0, vehicleBytes, 0, Math.min(srcBytes.length, 21));
                baos.write(vehicleBytes);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }

    @Override
    public String toString() {
        // 创建时间格式化对象，指定格式为 YYMMDDHHMMSS
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
        // 将 startTime 转换为 Date 类型并格式化
        String startTimeStr = sdf.format(new Date(startTime));
        // 将 endTime 转换为 Date 类型并格式化
        String endTimeStr = sdf.format(new Date(endTime));

        return "VehicleControlCommand{" +
                "commandType=" + commandType +
                ", startTime=" + startTimeStr +
                ", endTime=" + endTimeStr +
                ", regionId=" + regionId +
                ", vehicleNos=" + Arrays.toString(vehicleNos) +
                '}';
    }

    public static VehicleControlCommand fromByteArray(byte[] bytes) throws UnsupportedEncodingException {
        ByteBuffer buffer = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN);
        
        // 读取命令类型
        byte commandType = buffer.get();
        
        // 读取开始时间
        byte[] startTime = new byte[6];
        buffer.get(startTime);
        
        // 读取结束时间
        byte[] endTime = new byte[6];
        buffer.get(endTime);
        
        // 读取区域ID
        int regionId = buffer.getInt();
        
        // 读取车辆列表
        int remaining = buffer.remaining();
        int vehicleCount = remaining / 21;
        String[] vehicleNos = new String[vehicleCount];
        for (int i = 0; i < vehicleCount; i++) {
            byte[] vehicleBytes = new byte[21];
            buffer.get(vehicleBytes);
            vehicleNos[i] = new String(vehicleBytes, "GBK").trim();
        }
        
        return new VehicleControlCommand(commandType, JT809Utils.byteArrayToLong(startTime) , JT809Utils.byteArrayToLong(endTime) , regionId, vehicleNos);
    }
}
